/////////////////////////////////////////////////////////////////////////////
// Filename: ShadingModels.h
// Description: Implementation of anisotropic phong illumination
/////////////////////////////////////////////////////////////////////////////

#ifndef SHADING_MODELS_H
#define SHADING_MODELS_H

#include "Color.h"
#include "Vector3.h"
#include "Light.h"
#include "math.h"
#include <iostream>
#include <algorithm>
using namespace std;

Color AnisotropicPhong(Vector3 p, Vector3 N, Vector3 anisotropy_direction, Light l ){
	// ASSIGNMENT 3

	//TODO: add a switch between Flat and Interpolated/Gouraud shading.


	//switches to turn on/off the different parts of the Phong model (ambient, diffuse and specular)
	float a_on = 1.0;
	float d_on = 1.0;
	float s_on = 1.0;

	//reflection coefficients (I set them manually here - don't know if this is correct)
	float k_a = 0.50; //ambient
	float k_d = 0.50; //diffuse
	float k_s = 0.50; //specular

	//alpha x and y
	int alpha_x = 100;
	int alpha_y = 5;
	
	Vector3 L = l.position - p; //light vector
	L = L.normalize();
	Vector3 R = 2.0 * (L * N) * N - L; //reflection vector
	R = R.normalize();
	Vector3 V = Vector3(0,0,0) - p;
	V = V.normalize();

	//compute vector D (direction from lightsource to viewer)
	Vector3 D2 = (V - L);
	D2 = D2.normalize();
	Vector3 D = (D2 - (D2 * N)*N);
	D = D.normalize();
	
	//computer specular exponent a
	float t = pow(anisotropy_direction * D, 2);
	float alpha = t*alpha_x + (1 - t)*alpha_y;

	//ambient reflection
	Color I_a = k_a * l.ambient_color;

	//diffuse reflection
	float temp = N * L;
	if(temp < 0) {temp = 0.0;} //max(N*L, 0)
	Color I_d = k_d * l.diffuse_color * temp;

	//specular reflection
	temp = V * R;
	if(temp < 0) {temp = 0.0;} //max( (V*R)^a, 0 )
	Color I_s = k_s * l.specular_color * pow(temp, alpha);

	//Phong
	float a = 0.1;
	float b = 0.1;
	float c = 0.1;
	float d = p.abs();
	float att = 1 / (a + b*d + c*pow(d,2));
	//float att = 1.0;

	Color I = I_a * a_on + att * (I_d * d_on + I_s * s_on);
	//Color I = I_a + att * (I_d + I_s);

	return I;
	//return Color(0,0,0);
}

#endif

/////////////////////////////////////////////////////////////////////////////
//                               END OF FILE                               //
/////////////////////////////////////////////////////////////////////////////
